package com.zhulei.biji.suanfa.pace;

/**
 * @description: 有序列表合并
 * @author: zhulei
 * @create: 2025-04-22 17:13
 **/
public class LinkendMarge {
    static class Node{
        int value;
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    public static Node marge(Node n1,Node n2){
        Node temp1 = n1;
        Node temp2 = n2;
        Node temp = n1;
        while (temp1 != null || temp2 != null){
            if (temp1.value < temp2.value){

                Node n111 = new LinkendMarge.Node(temp2.value,temp1.next);
                temp1.next = n111;


            }else {
                temp = temp2.next;

            }
        }
        return  temp;
    }
    public static void printNode(Node n){
        System.out.println(n.value);
        if (n.next != null)
            printNode(n.next);
    }
    public static void main(String[] args) {
        Node n111 = new LinkendMarge.Node(6,null);
        Node n222 = new LinkendMarge.Node(5,null);

        Node n11 = new LinkendMarge.Node(4,n111);
        Node n22 = new LinkendMarge.Node(3,n222);

        Node n1 = new LinkendMarge.Node(2,n11);
        Node n2 = new LinkendMarge.Node(1,n22);

        printNode(n1);
        printNode(n2);


        printNode(marge(n1,n2));



    }
}
